(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.company.createController', createController);
	createController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'companyService', '$filter' ];
	function createController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, companyService, $filter) {
		var ctrl = this, companyInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.company = {};
		
		ctrl.init = function() {
			$scope.setPageDescription(function() {
				return ctrl.company ? ctrl.company.companyCode : null;
			});
			// init services
			companyService.initService($scope, ctrl);
			// init contacts
			companyService.initContact($scope, ctrl);
			// init subcompany
			companyService.initSubCompany($scope, ctrl);
			// init notify tab
			companyService.initNotify($scope, ctrl);
			// init company report
			companyService.initReport($scope, ctrl);
			// init company address
			companyService.initAddress($scope, ctrl);
			// init company dynaparm
			companyService.initDynaParm($scope, ctrl);
			companyService.initEdiProfileGrid($scope, ctrl);
			
			authService.create().then(function(response) {
				companyService.initGeneral($scope, ctrl, response.data, true).then(function(){
					ctrl.company = response.data || companyInfo;
					ctrl.company.services = ctrl.company.services || [];
					ctrl.company.subCompanies = ctrl.company.subCompanies || [];
					ctrl.company.notifies = ctrl.company.notifies || [];
					ctrl.company.companyRpts = ctrl.company.companyRpts || [];
					ctrl.company.companyAddrs = ctrl.company.companyAddrs || [];
					// init company ediprofile
					companyService.initEdiProfile($scope, ctrl);
					//init company classificationCode
					if (ctrl.classifiOptions && ctrl.classifiOptions.data && ctrl.classifiOptions.data[0]) {
						ctrl.company.classificationCode = ctrl.company.classificationCode || ctrl.classifiOptions.data[0].value;
					}
					//
					angular.forEach(ctrl.company.companyRpts, function(companyRpt, key) {
						if (!ctrl.company['companyRpts' + companyRpt.report.serviceCode]) {								
							ctrl.company['companyRpts' + companyRpt.report.serviceCode] = [];
						}
						ctrl.company['companyRpts' + companyRpt.report.serviceCode].push(companyRpt);
					});
					ctrl.resetDynaProperties();
				});
			});
		};

		ctrl.createSave = function() {
			if (ctrl.company.taskId) {
				return authService.createContinueSave(ctrl.company).then(saveSuccessful, saveFailed);
			} else {
				return authService.createSave(ctrl.company).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			if (ctrl.company.taskId) {
				return authService.createContinueSubmit(ctrl.company).then(submitSuccessful, submitFailed);
			} else {
				return authService.createSubmit(ctrl.company).then(submitSuccessful, submitFailed);
			}
		};
		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.company.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.company.list',
					'state' : 'cda.company.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
